<template>
	<view class="release-box">
		<view class="list" v-for="(item,index) of data" :key="index" @click="goPage('/pages/mutual/detail/detail?id='+item.id,1)">
			<view class="list-top">
				<view class="list-top-left">
					<span>{{item.name}}</span>
					<span>{{item.cate_name}}</span>
				</view>
				<view class="list-top-right" v-if="item.status===2" @click.stop="del(item.id)">
					<span>删除悬赏</span>
				</view>
			</view>
			<view class="list-content">
				<text>{{item.title}}</text>
			</view>
			<view class="list-bottom">
				<text>单价：{{item.price}}zkb/人</text>
				<text>剩余名额：{{item.quota-item.use_quota}}人</text>
				<text>评分：{{item.evaluate}}</text>
				<text>浏览：{{item.browse}}</text>
			</view>
			<view class="list-btns list-btns2" v-if="item.status===1">
				<view v-if="item.recommend" class="btn">
					<span>已推荐</span>
				</view>
				<view v-else class="btn" @click.stop="recommend(item.id,item.cid,item.r_price)">
					<span>推荐悬赏 ({{item.r_price}}zkb/h)</span>
				</view>
				<view class="btn" @click.stop="open(item.id)">
					<span>暂停悬赏</span>
				</view>
			</view>
			<view class="list-btns" v-if="item.status===2">
				<view class="btn" @click.stop="goPage('/pages/mutual/reward-edit/reward-edit?id='+item.id,'price')">
					<span>增加金额</span>
				</view>
				<view class="btn" @click.stop="goPage('/pages/mutual/reward-edit/reward-edit?id='+item.id,'quota')">
					<span>增加名额</span>
				</view>
				<view class="btn" @click.stop="open(item.id)">
					<span>开启悬赏</span>
				</view>
			</view>
			<view class="list-bottom-yy" v-if="item.status===3">
				<text>{{item.examine_fail}}</text>
			</view>
			<view class="list-btns list-btns2" v-if="item.status===3">
				<view class="btn" @click.stop="goLink(item.id)">
					<span>编辑悬赏</span>
				</view>
				<view class="btn" @click.stop="del(item.id)">
					<span>删除悬赏</span>
				</view>
			</view>
		</view>
		<block-loading :status="status" :len="data.length"></block-loading>
	</view>
</template>

<script>
	import {lists,updateStatus,delReward,recommendReward} from "@/api/reward.js"
	import {mapActions} from "vuex";
	export default { 
		name: "release-box",
		data() {
			return {
				cate: "",
				titleTabar: "",
				data:[],
				page:1,
				refresh:false,
				status:"nomal"
			}
		},
		onLoad(option) {
			this.titleTabar = option.titleTabar;
			this.cate = option.cate;
			this.getData();
		},
		//下拉刷新
		onPullDownRefresh() {
			this.status = "nomal"
			this.data = [];
			this.page = 1;
			this.refresh = true;
			this.getData()
		},
		//上拉加载
		onReachBottom(){
			this.getData()
		},
		methods: {
			...mapActions(["setUserInfo_action"]),
			goPage(url,type){
				uni.navigateTo({
					url:url+'&type='+type
				})
			},
			//去编辑悬赏
			goLink(id){
				uni.navigateTo({
					url:'/pages/mutual/release-next/release-next?titleTabar=编辑悬赏&reward_id='+id
				})
			},
			async getData(){
				let res = await lists({status:this.cate,page:this.page,size:20});
				if(this.refresh){
					uni.stopPullDownRefresh();
					this.refresh = false;
				}
				if(this.page===1&&res.data.length === 0){
					this.status = "nodata"
					return
				}
				if(this.page===1&&res.data.length > 0 && res.data.length < 20){
					this.status = "nomore"
				}
				if(res.data.length === 0){
					this.status = "nomore"
					return;
				}
				
				this.page++;
				this.data = this.data.concat(res.data);
			},
			// 开启/暂停悬赏
			async open(id){
				let res = await updateStatus({id:id})
				if(res.code === 1){
					this.data = this.data.filter((item)=>{
						return item.id !== id
					})
					if(this.data.length === 0){
						this.status = "nodata"
					}
				}else{
					this.$msg(res.msg)
				}
			},
			//推荐悬赏
			recommend(id,cid,price){
				let that = this;
				uni.showModal({
					title: '提示',
					content: `确定要支付 ${price}zkb 进行推荐该悬赏`,
					confirmColor: "#000000",
					success: async function(res) {
						if (res.confirm) {
							let res = await recommendReward({id:id,cid:cid})
							if(res.code === 1){
								that.data.forEach((item)=>{
									if(item.id === id){
										item.recommend = 1;
									}
								})
								that.setUserInfo_action();
							}else{
								that.$msg(res.msg)
							}
						}
					}
				})
			},
			//删除悬赏
			async del(id){
				let res = await delReward({id:id})
				if(res.code === 1){
					this.data = this.data.filter((item)=>{
						return item.id !== id
					})
					if(this.data.length === 0){
						this.status = "nodata"
					}
				}else{
					this.$msg(res.msg)
				}
			}
		},
		watch: {
			titleTabar: {
				handler(newValue) {
					uni.setNavigationBarTitle({
						title: newValue
					})
				},
				immediate: true
			}
		}
	}
</script>

<style scoped lang="less" src="./release-box.less">
</style>
